package com.chucang.shucang.ttp.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.chucang.shucang.common.base.constant.CommonConstant;
import com.chucang.shucang.common.base.constant.SecurityConstant;
import com.chucang.shucang.common.base.exception.FastException;
import com.chucang.shucang.common.base.utils.OpsUtil;
import com.chucang.shucang.common.base.utils.R;
import com.chucang.shucang.common.mq.constant.RabbitConstant;
import com.chucang.shucang.common.mq.to.NFTPublishTO;
import com.chucang.shucang.ttp.feign.PmsService;
import com.chucang.shucang.ttp.feign.UserService;
import com.chucang.shucang.ttp.feign.vo.UserCollectionHoldEntity;
import com.chucang.shucang.ttp.nftcenter.model.dto.DealResultDTO;
import com.chucang.shucang.ttp.nftcenter.model.req.NFTPublishReq;
import com.chucang.shucang.ttp.nftcenter.proxy.AvataProxy;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/17 22:10
 * @description nft发售
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RabbitListener(queues = RabbitConstant.NFT_PUBLISH_NFT_QUEUE)
public class NFTPublishListener {
    private final AvataProxy avataProxy;

    private final PmsService pmsService;

    private final UserService userService;

    @RabbitHandler
    public void nftPublishListener(NFTPublishTO nftPublishTO, Channel channel, Message message) throws IOException {
        log.info("发布藏品！");
        try {
            //处理藏品订单
            NFTPublishReq nftPublishReq = new NFTPublishReq();
            BeanUtil.copyProperties(nftPublishTO, nftPublishReq);
            avataProxy.publishNFT(nftPublishReq);
            //查询上链结果
            DealResultDTO dealResultDTO = avataProxy.queryDealResult(nftPublishTO.getOperation_id());
            log.info("藏品上链结果====》{}", dealResultDTO);
            Long collectionId = Long.valueOf(CharSequenceUtil.removePrefix(nftPublishTO.getClass_id(), CommonConstant.CLASS_ID_PREFIX));

            if (nftPublishTO.getSourceType() == 3) {
                //保存对应信息到我的藏品
                UserCollectionHoldEntity userCollectionHoldEntity = new UserCollectionHoldEntity();
                userCollectionHoldEntity.setCollectionId(collectionId);
                userCollectionHoldEntity.setUserId(nftPublishTO.getUserId());
                userCollectionHoldEntity.setNftId(dealResultDTO.getNft_id());
                userCollectionHoldEntity.setSourceType(nftPublishTO.getSourceType());
                userCollectionHoldEntity.setSerialNum(Integer.valueOf(nftPublishTO.getName()));

                userService.addCollectionHold(userCollectionHoldEntity, SecurityConstant.FROM_IN);
                //同步库存数据
                R<String> r = pmsService.updateCollectionSyntheticSurplus(collectionId, SecurityConstant.FROM_IN);
                OpsUtil.of(r)
                        .assertCode(CommonConstant.SUCCESS, e -> new FastException("更新库存失败！"));
            } else if (nftPublishTO.getSourceType() == 0) {
                UserCollectionHoldEntity userCollectionHoldEntity = new UserCollectionHoldEntity();
                userCollectionHoldEntity.setId(Long.valueOf(nftPublishTO.getOperation_id()));
                userCollectionHoldEntity.setNftId(dealResultDTO.getNft_id());
                userService.updateCollectionHold(userCollectionHoldEntity, SecurityConstant.FROM_IN);
            } else if (nftPublishTO.getSourceType() == 1) {
                //开盲盒出来的
                UserCollectionHoldEntity userCollectionHoldEntity = new UserCollectionHoldEntity();
                userCollectionHoldEntity.setCollectionId(collectionId);
                userCollectionHoldEntity.setUserId(nftPublishTO.getUserId());
                userCollectionHoldEntity.setNftId(dealResultDTO.getNft_id());
                userCollectionHoldEntity.setSourceType(nftPublishTO.getSourceType());
                userCollectionHoldEntity.setSerialNum(Integer.valueOf(nftPublishTO.getName()));

                userService.addCollectionHold(userCollectionHoldEntity, SecurityConstant.FROM_IN);
            }
            //TODO 上链信息记录日志表

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("发布藏品异常！");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
